package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo> implements ApartmentInfoService {

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private ApartmentLabelService apartmentLabelService;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private ProvinceInfoService provinceInfoService;

    @Autowired
    private CityInfoService cityInfoService;

    @Autowired
    private DistrictInfoService districtInfoService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;

    @Autowired
    private ApartmentFacilityMapper apartmentFacilityMapper;

    @Autowired
    private ApartmentFeeValueMapper apartmentFeeValueMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Override
    public IPage<ApartmentItemVo> pageApartments(IPage<ApartmentItemVo> iPage, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageApartments(iPage, queryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailVoById(Long id) {
        ApartmentInfo apartmentInfo = this.getById(id);
        if(apartmentInfo == null)
            return null;
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);

        apartmentDetailVo.setGraphVoList(graphInfoMapper.getGraphVoListByItemId(id, ItemType.APARTMENT));

        apartmentDetailVo.setLabelInfoList(apartmentLabelMapper.getLabelInfoListByApartmentId(id));

        apartmentDetailVo.setFacilityInfoList(apartmentFacilityMapper.getFacilityInfoListByApartmentId(id));

        apartmentDetailVo.setFeeValueVoList(apartmentFeeValueMapper.getFeeValueVoListByApartmentId(id));

        return apartmentDetailVo;
    }

    @Override
    public void removeApartmentById(Long id) {
        LambdaQueryWrapper<RoomInfo> roomQueryWrapper = new LambdaQueryWrapper<>();
        roomQueryWrapper.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoMapper.selectCount(roomQueryWrapper);
        if (count > 0) {
            return;
        }

        this.removeById(id);

        ApartmentSubmitVo apartmentSubmitVo = new ApartmentSubmitVo();
        apartmentSubmitVo.setId(id);
        this.deleteApartmentTagList(apartmentSubmitVo);
    }

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        boolean isUpdate = apartmentSubmitVo.getId() == null ? false : true;
        if(isUpdate){
            deleteApartmentTagList(apartmentSubmitVo);
        }

        apartmentSubmitVo.setProvinceName(provinceInfoService.getById(apartmentSubmitVo.getProvinceId()).getName());

        apartmentSubmitVo.setCityName(cityInfoService.getById(apartmentSubmitVo.getCityId()).getName());

        apartmentSubmitVo.setDistrictName(districtInfoService.getById(apartmentSubmitVo.getDistrictId()).getName());

        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if(graphVoList.size() > 0){
            List<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }

        //使用反射实现添加和修改
        try {
            reflectUpdateMethod(apartmentSubmitVo.getFacilityInfoIds(), ApartmentFacility.class, apartmentSubmitVo,apartmentFacilityService);

            reflectUpdateMethod(apartmentSubmitVo.getLabelIds(),ApartmentLabel.class,apartmentSubmitVo,apartmentLabelService);

            reflectUpdateMethod(apartmentSubmitVo.getFeeValueIds(),ApartmentFeeValue.class,apartmentSubmitVo,apartmentFeeValueService);
        }catch (Exception e){
            e.printStackTrace();
        }

        this.saveOrUpdate(apartmentSubmitVo);
    }

    //删除公寓标签列表
    public void deleteApartmentTagList(ApartmentSubmitVo apartmentSubmitVo){
        LambdaQueryWrapper<GraphInfo> graphInfoWrapper = new LambdaQueryWrapper<>();
        graphInfoWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT).eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
        graphInfoService.remove(graphInfoWrapper);

        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
        apartmentFacilityService.remove(apartmentFacilityWrapper);

        LambdaQueryWrapper<ApartmentLabel> apartmentLabelWrapper = new LambdaQueryWrapper<>();
        apartmentLabelWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
        apartmentLabelService.remove(apartmentLabelWrapper);

        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
        apartmentFeeValueService.remove(apartmentFeeValueWrapper);
    }

    //通过反射剥离出添加或修改的重复操作
    public <T,U extends IService> void reflectUpdateMethod(List<Long> list, Class<T> clazz, ApartmentSubmitVo apartmentSubmitVo, U u) throws Exception {
        if(list.size() > 0){
            List<T> tList = new ArrayList<>();
            for (Long l : list) {
                Field[] declaredField = clazz.getDeclaredFields();
                T t = clazz.newInstance();
                for (Field field : declaredField) {
                    field.setAccessible(true);
                    String fieldName = field.getName();
                    if(fieldName.equals("apartmentId")){
                        field.set(t,apartmentSubmitVo.getId());
                    } else if (fieldName.equals("serialVersionUID")) {
                        continue;
                    } else {
                        field.set(t,l);
                    }
                }
                tList.add(t);
            }
            u.saveBatch(tList);
        }
    }
}




